// pages/jingcai_db/jingcai_db.js
import { request } from "../../utils/http.js";
import { time } from "../../utils/time.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    judge:true,
    in:0,
    id:0,
    kecheng:{},
    alltime:{
      shi:0,
      fen:0
    },
    xuzhi:[],
    list:[],
    teacher:{},
    allnum:0,
    pingjia:[],
    pingnum:0,
    jiaoliu:{},
    huifu:{},
    hui:1,
    buy:0,
  },

  ba(){
    wx.navigateBack();
  },

  lok(event){
    this.setData({
      judge: false
    });
    const id = event.currentTarget.dataset.id;
    request({
      url:`huifu/listid?jId=${id}`
    }).then((data)=>{
      this.setData({
        huifu:data.map((item)=>{
          item.shijian = time({
            time:item.hTime
          })
          return item;
        })
      })
    }).catch((err)=>{
      console.log(err);
    })
  },

  see(){
    this.setData({
      judge: true
    })
  },

  jinit(id){
    this.setData({
      id:id
    })
  },

  look(event){
    const ins = event.currentTarget.dataset.in;
    this.setData({
      in:ins
    })
  },

  changehui(event){
    const id = event.currentTarget.dataset.id;
    this.setData({
      hui:id
    })
  },

  tobuy(){
    let app = getApp();
    let info = app.globalData.userInfo;
    let ids = info[0].id;
    request({
      url:'buy/add',
      method:'POST',
      header:{
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        uId:ids,
        kId:this.data.id
      }
    }).then(()=>{
      this.isbuy();
    }).catch((err)=>{
      console.log(err);
    })
    this.isbuy();
  },

  isbuy(){
    let app = getApp();
    let info = app.globalData.userInfo;
    let ids = info[0].id;
    request({
      url:`buy/findid?uId=${ids}&kId=${this.data.id}`,
    }).then((data)=>{
        this.setData({
          buy:data ? 1 : 0
        })
    }).catch((err)=>{
      console.log(err);
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const id = options.id;
    this.jinit(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    request({
      url:`kecheng/findid?id=${this.data.id}`,
    }).then((data)=>{
      this.setData({
        kecheng:data,
        xuzhi:data.xiangQingDb.xXuzhi == null ? null : data.xiangQingDb.xXuzhi.split("/"),
        alltime:{
          shi:Math.floor(data.allTime/60),
          fen:data.allTime%60
        }
      });
      request({
        url:`teacher/findid?id=${data.kTid}`,
      }).then((data)=>{
        this.setData({
          teacher:data,
        });
      }).catch((err)=>{
        console.log(err);
      })
    }).catch((err)=>{
      console.log(err);
    }),
    request({
      url:`list/listid?id=${this.data.id}`,
    }).then((data)=>{
      this.setData({
        list:data,
        allnum:data.length
      })
    }),
    request({
      url:`pingjia/listid?id=${this.data.id}`,
    }).then((data)=>{
      this.setData({
        pingnum:data.length,
        pingjia:data.map((item)=>{
          item.shijian = time({
            time:item.pTime
          })
          return item;
        })
      })
    }),
    request({
      url:`jiaoliu/listid?id=${this.data.id}&order=${this.data.hui}`,
    }).then((data)=>{
      this.setData({
        jiaoliu:data.map((item)=>{
          item.shijian = time({
            time:item.jTime
          })
          return item;
        })
      })
    })
    this.isbuy();
  },

  toXiangXi(event){
    const id = event.target.dataset.id;
    wx.navigateTo({
      url: `/pages/xiangxi_db/xiangxi_db?id=${id}`,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})